﻿using System.Windows.Controls;
using System.Windows;
using DeepEarth.Core.Controls;
using System.Collections.ObjectModel;
using DeepEarth.Toolkit.Common.Controls;
namespace DeepEarth.Toolkit.Controls
{
    // what is this providing over a templated list box?
    // It was intended to maintain a collection of templated list boxes - but is that still required?
    public class ObjectVisualizer : Control
    {
        const string PART_TemplatedListBox = "PART_TemplatedListBox";

        public static readonly DependencyProperty ItemTemplateProperty =
            DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(ObjectVisualizer), null);

        public DataTemplate ItemTemplate
        {
            get { return (DataTemplate)GetValue(ItemTemplateProperty); }
            set { SetValue(ItemTemplateProperty, value); }
        }

        public static readonly DependencyProperty TemplateSelectorProperty =
            DependencyProperty.Register("TemplateSelector", typeof(TemplateSelector), typeof(ObjectVisualizer), null);

        public TemplateSelector TemplateSelector
        {
            get { return (TemplateSelector)GetValue(TemplateSelectorProperty); }
            set { SetValue(TemplateSelectorProperty, value); }
        }

        public static readonly DependencyProperty ItemsSourceProperty =
            DependencyProperty.Register("ItemsSource", typeof(ObservableCollection<object>), typeof(ObjectVisualizer), null);

        public ObservableCollection<object> ItemsSource
        {
            get { return (ObservableCollection<object>)GetValue(ItemsSourceProperty); }
            set { SetValue(ItemsSourceProperty, value); }
        }

        public ItemCollection Items
        {
            get
            {
                if (templatedListBox != null)
                {
                    return templatedListBox.Items;
                }
                else
                {
                    return null;
                }
            }
        }

        public ObjectVisualizer()
        {
            DefaultStyleKey = typeof(ObjectVisualizer);
        }

        private TemplatedListBox templatedListBox;

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            templatedListBox = (TemplatedListBox)GetTemplateChild(PART_TemplatedListBox);
        }
    }
}
